package com.shiikong_memory.yez.Arknights.ArknightsData;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class AgentList {
    private static Random random = new Random();
    private List<Agent> agentList;

    public AgentList(JSONArray AssistChars, JSONArray Chars){
        agentList = new ArrayList<>();
        JSONObject json = Chars.getJSONObject(0);
        for (int i=0;i<AssistChars.size();i++){
            JSONObject assistChars = AssistChars.getJSONObject(i);
            if(assistChars.getInteger("evolvePhase") == 2){
                String charId = assistChars.getString("charId");
                String name = json.getJSONObject(charId).getString("name");
                AgentOccupation occupation = AgentOccupation.getAgentOccupation(json.getJSONObject(charId).getString("profession"));
                Agent agent = new Agent(charId, name, occupation);
                agentList.add(agent);
            }
        }
    }

    public Agent getRandomAgent(){
        return agentList.get(random.nextInt(agentList.size()));
    }

    public List<Agent> getNoRandomAgent(int i, Agent agent){
        if (agentList.size()<=i){
            return null;
        }
        List<Agent> agents = new ArrayList<>(agentList);
        List<Agent> list = new ArrayList<>();
        for (int j=0;j<i;j++){
            Agent a = agents.get(random.nextInt(agents.size()));
            list.add(a);
            agents.remove(a);
        }
        return list;
    }

    @Data
    @AllArgsConstructor
    public class Agent{
        //干员id
        private String id;
        //干员名称
        private String name;
        //职业类型
        private AgentOccupation Occupation;
    }
    public enum AgentOccupation{
        //先锋
        VANGUARD("PIONEER", "先锋"),
        //近卫
        GUARD("WARRIOR", "近卫"),
        //重装
        DEFENDER("TANK", "重装"),
        //狙击
        SNIPER("SNIPER", "狙击"),
        //术师
        CASTER("CASTER", "术师"),
        //医疗
        MEDIC("MEDIC", "医疗"),
        //辅助
        SUPPORTER("SUPPORT", "辅助"),
        //特种
        SPECIALIST("SPECIAL", "特种");
        @Getter
        private String ID;
        private String ChineseName;

        AgentOccupation(String ID, String ChineseName){
            this.ID = ID;
            this.ChineseName = ChineseName;
        }

        public static AgentOccupation getAgentOccupation(String ID){
            return switch (ID){
                case "PIONEER" -> AgentOccupation.VANGUARD;
                case  "WARRIOR" -> AgentOccupation.GUARD;
                case "TANK" -> AgentOccupation.DEFENDER;
                case "SNIPER" -> AgentOccupation.SNIPER;
                case "CASTER" -> AgentOccupation.CASTER;
                case "MEDIC" -> AgentOccupation.MEDIC;
                case "SUPPORT" -> AgentOccupation.SUPPORTER;
                case "SPECIAL" -> AgentOccupation.SPECIALIST;
                default -> throw new IllegalStateException("Unexpected value: " + ID);
            };
        }

        @Override
        public String toString() {
            return ChineseName;
        }
    }
}
